<%@page import="com.xxb.dao.ProductDao"%>
<%@page import="com.xxb.entity.Product"%>
<%@page import="com.xxb.entity.CartItem"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String id = request.getParameter("id");
	String num = request.getParameter("num");
	
	Product p = new ProductDao().findByid(Integer.parseInt(id));
	CartItem item = new CartItem();
	item.setProduct(p);
	item.setNum(Integer.parseInt(num));
	
	
	List<CartItem> shopcar = (List<CartItem>)session.getAttribute("shopcar");
	if(shopcar == null){
		shopcar = new  ArrayList<CartItem>();
	}
	
	if(shopcar.size() == 0){
		shopcar.add(item);	
	}else{ 
		//循环购物车集合，判断集合中是否存在现在要添加的产品
		boolean flag = true;
		for(CartItem cartItem : shopcar){
			if(item.getProduct().getId() == cartItem.getProduct().getId()){
				cartItem.setNum(cartItem.getNum() + Integer.parseInt(num));
				flag = false;
			}
		}
		
		if(flag){
			shopcar.add(item);
		}
	}
	
	session.setAttribute("shopcar",shopcar);
	response.sendRedirect("cart.jsp");
%>